<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
          <el-form-item label="类型名称" prop="typeCode">
            <el-input v-model="queryParams.typeCode" placeholder="请输入编码" clearable style="width:100px;" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="类型名称" prop="typeName">
            <el-input v-model="queryParams.typeName" placeholder="请输入名称" clearable style="width:100px;" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['iotoperation:DeviceType:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['iotoperation:DeviceType:edit']" >修改</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['iotoperation:DeviceType:remove']" >删除</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['iotoperation:DeviceType:export']" >导出</el-button>
          </el-col> -->
          <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading" :data="DeviceTypeList" highlight-current-row @row-click="dataTableClick">

          <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
          <el-table-column label="序号" type="index" />
          <!-- <el-table-column label="图标" align="center" prop="icon" /> -->
          <el-table-column label="类型编码" align="center" prop="typeCode" />
          <el-table-column label="类型名称" align="center" prop="typeName" />
          <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-hasPermi="['iotoperation:DeviceType:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button v-hasPermi="['iotoperation:DeviceType:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改设备大类对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="类型编码" prop="typeCode">
              <el-input v-model="form.typeCode" placeholder="请输入类型编码" />
            </el-form-item>
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" />
            </el-form-item>
            <!-- <el-form-item label="图标" prop="icon">
              <el-input v-model="form.icon" placeholder="请输入图标" />
            </el-form-item> -->
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
      <el-col :span="16">
        <div>
          <device-type-use ref="DeviceTypeUse" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listDeviceType, getDeviceType, delDeviceType, addDeviceType, updateDeviceType } from "@/api/iotoperation/DeviceType";
import DeviceTypeUse from "../DeviceTypeUse/index";

export default {
  name: "DeviceType",
  components: { DeviceTypeUse },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备大类表格数据
      DeviceTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeCode: [{ required: true, message: "类型编码不能为空", trigger: "blur" }],
        typeName: [{ required: true, message: "类型名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备大类列表 */
    getList() {
      this.loading = true;
      listDeviceType(this.queryParams).then(response => {
        this.DeviceTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        icon: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.dataTableReload();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.dataTableReload();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备大类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备大类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备大类编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download("iotoperation/DeviceType/export", {
        ...this.queryParams
      }, `DeviceType_${new Date().getTime()}.xlsx`)
    },
    /** 表格行单击操作 */
    dataTableClick(row, column, event) {
      this.$refs.DeviceTypeUse.loadDataByType(row);
    },
    /** 表格刷新操作 */
    dataTableReload() {
      this.$refs.DeviceTypeUse.loadDataByType();
    }
  }
};
</script>
